package com.yjhh.chdataservice.controller;

import com.google.gson.Gson;
import com.yjhh.chdataservice.beans.CommonNameValueVo;
import com.yjhh.chdataservice.beans.KeyStatisticalCondVo;
import com.yjhh.chdataservice.except.YJHHException;
import com.yjhh.chdataservice.service.FZJCKeyInfoService;
import com.yjhh.chdataservice.util.CommonResultData;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * Created by hlantian on 2017/9/26.
 */
@Api(tags = "大数据模块关键指标相关接口")
@RestController
@RequestMapping(value = "/service/fzjc/keyinfo")
@Log4j2
public class FZJCKeyInfoController {

    @Autowired
    private FZJCKeyInfoService fzjcKeyInfoService;

    @ApiOperation(value = "开工率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计", notes = "开工率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\",\"endDate\":\"2017-12-24\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getPointStartedRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getPointStartedRate(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例
            statisticalData = fzjcKeyInfoService.getPointStartedOrEndedRate(keyStatisticalCondVo, "a_startprojecttime");
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getPointStartedRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

    @ApiOperation(value = "竣工率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计", notes = "竣工率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\",\"endDate\":\"2017-12-24\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getPointEndedRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getPointEndedRate(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例
            statisticalData = fzjcKeyInfoService.getPointStartedOrEndedRate(keyStatisticalCondVo, "a_endprojecttime");
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getPointEndedRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

    @ApiOperation(value = "交钥匙率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计", notes = "交钥匙率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\",\"endDate\":\"2017-12-24\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getObjTurnKeyRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getObjTurnKeyRate(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例
            statisticalData = fzjcKeyInfoService.getObjTurnKeyOrMoveInRate(keyStatisticalCondVo, "objmovesupinf.obj_movesup_jysj");
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getObjTurnKeyRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

    @ApiOperation(value = "入住率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计", notes = "入住率统计，按地市和三大片区进行统计，按开工时间截止日期进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\",\"endDate\":\"2017-12-24\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getObjMoveInRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getObjMoveInRate(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例
            statisticalData = fzjcKeyInfoService.getObjTurnKeyOrMoveInRate(keyStatisticalCondVo, "objmovesupinf.obj_movesup_sjrzsj");
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getObjMoveInRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }


    @ApiOperation(value = "集中安置率统计，按地市和三大片区进行统计", notes = "集中安置率统计，按地市和三大片区进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getCentralizedRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getCentralizedRate(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例

            TermQueryBuilder partTermQuery = termQuery("objmovesupinf.obj_movesup_azfs", "25");
            statisticalData = fzjcKeyInfoService.getCentralizedRate(keyStatisticalCondVo, partTermQuery);
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getCentralizedRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

    @ApiOperation(value = "楼房化率统计，按地市和三大片区进行统计", notes = "楼房化率，按地市和三大片区进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getCityRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getCityRate(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例

            String[] fwType = new String[]{"31", "32"};
            TermsQueryBuilder partTermQuery = termsQuery("objmovesupinf.obj_movesup_fwlx", fwType);
            statisticalData = fzjcKeyInfoService.getCentralizedRate(keyStatisticalCondVo, partTermQuery);
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getCityRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

    @ApiOperation(value = "脱贫率统计，按地市和三大片区进行统计", notes = "脱贫率统计，按地市和三大片区进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getTPRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getTPRate(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例

            TermQueryBuilder partTermQuery = termQuery("objmeasuresinfo.obj_measures_sftp", "是");
            statisticalData = fzjcKeyInfoService.getCentralizedRate(keyStatisticalCondVo, partTermQuery);
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getTPRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

    @ApiOperation(value = "人均年收入统计，按地市和三大片区进行统计", notes = "人均年收入统计，按地市和三大片区进行统计")
    @ApiImplicitParam(name = "statisticalParams", value = "统计指标json条件", required = true, paramType = "query", dataType = "String", defaultValue =
            "{\"statisticalType\":\"city\",\"areaCode\":\"610000000000\",\"cond\":\"610000000000\"}")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getPerCapitaInCome", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getPerCapitaInCome(@RequestParam String statisticalParams) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            KeyStatisticalCondVo keyStatisticalCondVo = new Gson().fromJson(statisticalParams, KeyStatisticalCondVo.class);//对于javabean直接给出class实例

            statisticalData = fzjcKeyInfoService.getPerCapitaInCome(keyStatisticalCondVo);
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getPerCapitaInCome", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

    /**
     * areaCode:查询的区域
     * indexType:指标类型 (1:开工率;2:竣工率;3:入住率;4:集中安置率)
     * moveType:搬迁类型(1:合计 2：易地扶贫 3：避灾 4：生态 5:避灾生态合计)
     * planYear:查询年份
     *
     * @return
     */
    @ApiOperation(value = "开工率;竣工率;入住率;集中安置率", notes = "开工率;竣工率;入住率;集中安置率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "行政区划编码", required = true, paramType = "query", dataType = "String", defaultValue =
                    "611000000000"),
            @ApiImplicitParam(name = "indexType", value = "指标类型 (1:开工率;2:竣工率;3:入住率;4:集中安置率)", required = true, paramType = "query", dataType = "String", defaultValue =
                    "1"),
            @ApiImplicitParam(name = "moveType", value = "搬迁类型(1:合计 2：易地扶贫 3：避灾 4：生态 5:避灾生态合计)", required = true, paramType = "query", dataType = "String", defaultValue =
                    "1"),
            @ApiImplicitParam(name = "planYear", value = "计划年份（可以查询合计年，合计年传入空字符串或者'all'）", required = true, paramType = "query", dataType = "String", defaultValue =
                    "2017")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/getReportKeyRate", method = RequestMethod.GET)
    public CommonResultData<List<CommonNameValueVo<Double>>> getReportKeyRate(@RequestParam String areaCode, @RequestParam String
            indexType, @RequestParam String moveType, @RequestParam String planYear) {
        CommonResultData<List<CommonNameValueVo<Double>>> statisticalData = null;
        try {
            long startTime = System.currentTimeMillis();         // 开始时间
            statisticalData = fzjcKeyInfoService.getReportKeyRate(areaCode,indexType,moveType,planYear);
            statisticalData.setRespTime(System.currentTimeMillis() - startTime);
        } catch (YJHHException e) {
            log.warn("getReportKeyRate", e);
            statisticalData = new CommonResultData<>(HttpStatus.OK.value(), "no data");
        }
        return statisticalData;
    }

}
